package comWorld.blog.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webManage.FileManage;

import com.sun.org.apache.bcel.internal.generic.NEW;

import comWorld.blog.BlogInfo;
import comWorld.blog.BlogInfoManage;
import comWorld.blogCatalog.BlogCatalogManage;
import comWorld.blogCatalog.BlogCatalogs;
import comWorld.blogComment.BlogCommentManage;
import comWorld.favorite.FavoriteManage;
import comWorld.social.SocialInfo;
import comWorld.social.SocialManage;
import comWorld.social.VisitorCount;
import comWorld.user.UserInfo;
import comWorld.user.UserStatus;
import comWorld.user.UserUrls;

/**
 * Servlet implementation class oneBlogPageInit
 */
@WebServlet("/blog/oneBlogPageInit")
public class oneBlogPageInit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public oneBlogPageInit() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserStatus visitorStatus = (UserStatus) request.getSession().getAttribute("visitorStatus");
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
		BlogCatalogs blogCatalogs = new BlogCatalogs(userInfo.getId());
		int privacy = 0;
		if (userInfo.getId() != visitorStatus.getId())
			privacy = 1;
		
		VisitorCount visitorCount = VisitorCount.getLastVisitorCount(userInfo.getId());
		visitorCount.add();
		request.setAttribute("visitorCount", visitorCount);
		
		blogCatalogs.statisticsBlogCount(userInfo.getId(), privacy);
		request.setAttribute("blogCatalogs", blogCatalogs);
		request.setAttribute("hotBlogs", BlogInfoManage.getHotBlogs(userInfo.getId()));
		SocialInfo socialInfo = (SocialInfo) request.getAttribute("socialInfo");
		
		int blogId = (int) request.getAttribute("blogId");
		BlogInfo blogInfo = BlogInfoManage.getBlogInfo(userInfo.getId(), blogId);
		if (blogInfo == null) {
			UserUrls userUrls = (UserUrls) request.getAttribute("userUrls");
			response.sendRedirect(userUrls.getBlog());
			return;
		}
		blogInfo.addReadCount();
		request.setAttribute("blogInfo", blogInfo);
		request.setAttribute("totalComment", blogInfo.getCommentCount());
		request.setAttribute("blogContent", BlogInfoManage.getBlogContent(blogId, userInfo.getId(), getServletContext()));
		
		int userId = (int) request.getAttribute("userId");
		String forward = "/blog/oneBlog.jsp";
		if (visitorStatus.getId() != userId) {
			request.setAttribute("followed", SocialManage.judgeUserHasFollowed(visitorStatus, socialInfo, userId));
			request.setAttribute("collected", FavoriteManage.judgeBlogHasCollected(visitorStatus, blogId));
			forward = "/blog/otherOneBlog.jsp";
		}
		request.getRequestDispatcher(forward).forward(request, response);
	}

}
